package com.onemile.app;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onemile.app.base.BaseUI;
import com.onemile.data.DXml;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.ECommodity;
import com.onemile.net.NetClient;
import com.onemile.net.NetImageLoaderM;
import com.onemile.utils.UtiDialogsFactory;

public class UICommodityDetails extends BaseUI
{
   private String commodityId;
   private ImageView imgBig;
   private TextView txtName;
   private TextView txtRemarks;
   private TextView txtPrice;
   private TextView txtPromotion;
   
   private TextView txtTopInfo;
   
   private void InitControls()
   {
	  imgBig = (ImageView) findViewById(R.id.imgCommodity_commoditydetails);
	  txtName = (TextView) findViewById(R.id.txtName_commoditydetails);
	  txtRemarks = (TextView) findViewById(R.id.txtRemarks_commoditydetails);
	  txtPrice = (TextView) findViewById(R.id.txtPrice_commoditydetails);
	  txtPromotion=(TextView)findViewById(R.id.txtPromotion_commoditydetails);
	  txtTopInfo=(TextView)findViewById(R.id.txtInfo_topinfobar);
	  txtTopInfo.setText(String.format("%s元起送，%s公里以内", Session.getInstance().getCurrentShop().getLowestPrice(), Session.getInstance().getCurrentShop().getDeliveryDistance()));
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  InitControls();
	  executeAsyncTask(PubConst.TaskName.COMMODITY_DETAILS);
   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.commoditydetails;
   }

   @Override
   public void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum)
   {
	  topTxt.setText(getString(R.string.title_commoditydetails));
	  commodityId = getIntent().getStringExtra(PubConst.Intent_Key.COMMODITY_ID);
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UICommodityDetails.this, getString(R.string.dialog_loading));
	  NetImageLoaderM imageLoaderM=new NetImageLoaderM();
	  Drawable d=imageLoaderM.loadDrawable(imgBig, commodityId, 2, false,new NetImageLoaderM.ImageCallback()
	  {
	     @Override
	     public void setImage(ImageView imageView, Drawable imageDrawable)
	     {
	  	   imageView.setImageDrawable(imageDrawable);  
	     }
	  });
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if (taskName.equals(PubConst.TaskName.COMMODITY_DETAILS))
	  {
		 try
		 {
			return NetClient.GetCommmodityDetails(commodityId);
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "UICommodityDetails_onAsyncTaskInBackground(taskName, params) ", e);
			}
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  UtiDialogsFactory.getInstance().DismissLoadingDialog();
	  if (taskName.equals(PubConst.TaskName.COMMODITY_DETAILS))
	  {
		 if (result != null)
		 {
            DXml data=(DXml)result;
            txtName.setText(data.getBody().GetFieldValue("CommodityName"));
            txtPrice.setText(data.getBody().GetFieldValue("Price"));
            txtRemarks.setText(data.getBody().GetFieldValue("CommodityRemarks"));
            txtPromotion.setText(data.getBody().GetFieldValue("PromotionName"));
		 }
	  }
   }

}
